package com.ljd.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.ljd.sevice.VodService;
import com.ljd.util.ConstantVodUtil;
import com.ljd.util.InitVodCilent;
import com.ljd.util.R;
import com.why.handler.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/eduvod/video")
//@CrossOrigin
public class VodController {
    @Autowired
    VodService vodService;

    @PostMapping("/uploadAlyVideo")
    public R uploadAlyVideo(MultipartFile file){
        String videoId = vodService.uploadAlyVideo(file);
        return R.ok().data("videoId",videoId);
    }
    @DeleteMapping("/deleteAlyVideo/{id}")
    public R deleteAlyVideo(@PathVariable String id){
        vodService.deleteAlyVideo(id);
        return R.ok();
    }
    @DeleteMapping("/deleteBatch")
    public R deleteBatch(@RequestParam List<String> videoIds){
        vodService.deleteBatch(videoIds);
        return R.ok();

    }
    @GetMapping("getPlayAuth/{videoId}")
    public R getVideoPlayAuth(@PathVariable String videoId) {
        try {
            //获取阿里云存储相关常量
            String accessKeyId = ConstantVodUtil.ACCESS_KEY_ID;
            String accessKeySecret = ConstantVodUtil.ACCESS_KEY_SECRET;

            // 初始化
            DefaultAcsClient client = InitVodCilent.initVodClient(accessKeyId,accessKeySecret);

            //
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            request.setVideoId(videoId);
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            // 得到凭证
            String playAuth = response.getPlayAuth();
            return R.ok().message("获取凭证成功").data("playAuth",playAuth);
        } catch (ClientException e) {
            e.printStackTrace();
            throw new GuliException(20001,"获取视频播放凭证失败");
        }
    }
}
